package com.lagou.mr.wc;


import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;


public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    private static final Logger log = LoggerFactory.getLogger(WordCountMapper.class);
    final Text k = new Text();
    final IntWritable v = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        // 1 对接受到的一行文本内容转换成String类型
        final String strValue = value.toString();


        // 2 将字符串按指定规则进行切割
        final String[] strArr= StringUtils.split(strValue," ");

        // 3 遍历数组，输出<单词，1>
        for (String str : strArr) {
            k.set(str);
            log.info("["+WordCountMapper.class.getName()+"]"+"- this word is :"+str);
            context.write(k,v);
        }
    }
}
